package com.wanwu.channel.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.wanwu.channel.notify.config.DruidProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Slf4j
@Configuration
@EnableConfigurationProperties(com.wanwu.channel.notify.config.DruidProperties.class)
public class DruidConfiguration {

    @Autowired
    private final com.wanwu.channel.notify.config.DruidProperties druidProperties;

    @Autowired
    public DruidConfiguration(DruidProperties druidProperties) {
        this.druidProperties = druidProperties;
    }

    @Bean
    @Primary
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        BeanUtils.copyProperties(druidProperties, dataSource);
        return dataSource;
    }
}
